<?php

/**
 * Stage form base class.
 *
 * @method Stage getObject() Returns the current form's model object
 *
 * @package    tfe
 * @subpackage form
 * @author     3emeIG
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $
 */
abstract class BaseStageForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                                   => new sfWidgetFormInputHidden(),
      'libelle'                              => new sfWidgetFormInputText(),
      'animateur'                            => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Animateur'), 'add_empty' => true)),
      'type'                                 => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('TypeArtistique'), 'add_empty' => false)),
      'type_de_public'                       => new sfWidgetFormChoice(array('choices' => array('enfants' => 'enfants', 'adultes' => 'adultes', 'ados' => 'ados', 'ados-adultes' => 'ados-adultes', 'tous publics' => 'tous publics'))),
      'age_minimum'                          => new sfWidgetFormInputText(),
      'age_maximum'                          => new sfWidgetFormInputText(),
      'lieu'                                 => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Lieu'), 'add_empty' => false)),
      'ouvert'                               => new sfWidgetFormInputCheckbox(),
      'date_de_debut'                        => new sfWidgetFormDate(),
      'date_de_fin'                          => new sfWidgetFormDate(),
      'nbr_personnes_max'                    => new sfWidgetFormInputText(),
      'nbr_personnes_min'                    => new sfWidgetFormInputText(),
      'heure_debut'                          => new sfWidgetFormTime(),
      'heure_fin'                            => new sfWidgetFormTime(),
      'prix_prevente'                        => new sfWidgetFormInputText(),
      'prix_sur_place'                       => new sfWidgetFormInputText(),
      'prix_membre'                          => new sfWidgetFormInputText(),
      'commentaire'                          => new sfWidgetFormTextarea(),
      'deleted_at'                           => new sfWidgetFormDateTime(),
      'created_at'                           => new sfWidgetFormDateTime(),
      'updated_at'                           => new sfWidgetFormDateTime(),
      'courriers_electroniques_envoyes_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'CourrierElectronique')),
    ));

    $this->setValidators(array(
      'id'                                   => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'id', 'required' => false)),
      'libelle'                              => new sfValidatorString(array('max_length' => 45)),
      'animateur'                            => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Animateur'), 'required' => false)),
      'type'                                 => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('TypeArtistique'))),
      'type_de_public'                       => new sfValidatorChoice(array('choices' => array(0 => 'enfants', 1 => 'adultes', 2 => 'ados', 3 => 'ados-adultes', 4 => 'tous publics'), 'required' => false)),
      'age_minimum'                          => new sfValidatorInteger(array('required' => false)),
      'age_maximum'                          => new sfValidatorInteger(array('required' => false)),
      'lieu'                                 => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Lieu'))),
      'ouvert'                               => new sfValidatorBoolean(array('required' => false)),
      'date_de_debut'                        => new sfValidatorDate(),
      'date_de_fin'                          => new sfValidatorDate(),
      'nbr_personnes_max'                    => new sfValidatorInteger(array('required' => false)),
      'nbr_personnes_min'                    => new sfValidatorInteger(array('required' => false)),
      'heure_debut'                          => new sfValidatorTime(array('required' => false)),
      'heure_fin'                            => new sfValidatorTime(array('required' => false)),
      'prix_prevente'                        => new sfValidatorNumber(array('required' => false)),
      'prix_sur_place'                       => new sfValidatorNumber(array('required' => false)),
      'prix_membre'                          => new sfValidatorNumber(array('required' => false)),
      'commentaire'                          => new sfValidatorString(array('required' => false)),
      'deleted_at'                           => new sfValidatorDateTime(array('required' => false)),
      'created_at'                           => new sfValidatorDateTime(),
      'updated_at'                           => new sfValidatorDateTime(),
      'courriers_electroniques_envoyes_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'CourrierElectronique', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('stage[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Stage';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['courriers_electroniques_envoyes_list']))
    {
      $this->setDefault('courriers_electroniques_envoyes_list', $this->object->CourriersElectroniquesEnvoyes->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveCourriersElectroniquesEnvoyesList($con);

    parent::doSave($con);
  }

  public function saveCourriersElectroniquesEnvoyesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['courriers_electroniques_envoyes_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->CourriersElectroniquesEnvoyes->getPrimaryKeys();
    $values = $this->getValue('courriers_electroniques_envoyes_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('CourriersElectroniquesEnvoyes', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('CourriersElectroniquesEnvoyes', array_values($link));
    }
  }

}
